//
//  activityOrderViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/23.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "activityOrderViewController.h"
#import "babySwimViewController.h"
#import "HttpRequest.h"
#import "tiaoZhuanView.h"
#import "nextActivityModel.h"
#import "buyNSObject.h"
#import "Macros.h"
#import "sureOrderViewController.h"
#import "swimToPhoto.h"
#import "TCLYUser.h"

//放入线下活动里的内容
#import "xianXiaDetailViewController.h"
#import "xianXiaDetail.h"
#import "SVProgressHUD.h"

#import "activityOrder.h"

@interface activityOrderViewController ()
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
//游泳卡的名字
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
//价格
@property (weak, nonatomic) IBOutlet UILabel *priceLabel01;

//textField里的数量
@property (weak, nonatomic) IBOutlet UITextField *textField;
//价格
@property (weak, nonatomic) IBOutlet UILabel *priceLabel02;
@property (weak, nonatomic) IBOutlet UILabel *phoneNumberLabel;
//价格
@property (weak, nonatomic) IBOutlet UILabel *priceLabel03;
//价格
@property (weak, nonatomic) IBOutlet UILabel *priceLabel04;
//购买按钮
@property (weak, nonatomic) IBOutlet UIButton *purchaseBtn;
@property(nonatomic,assign)int num;
@property(nonatomic,copy)NSString*urlString1;
@property(nonatomic,copy)NSMutableArray*json_Array1;
//钱数
@property(nonatomic,copy)NSString *ticket_IDString;

@property(nonatomic,copy)NSString *urlString2;
@property(nonatomic,copy)NSMutableArray *json_Array2;
@property(nonatomic,assign)float whichView;
@property(nonatomic,copy)NSDictionary *data_dic;
@property(nonatomic,retain)UIView *backView;
@end

@implementation activityOrderViewController
@synthesize json_Array1,json_Array2,data_dic;
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=YES;
    self.navigationController.navigationBar.hidden=YES;
}
static float progress = 0.0f;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.num=1;
    json_Array1=[NSMutableArray array];
    json_Array2=[NSMutableArray array];
    self.textField.text=[NSString stringWithFormat:@"%d",self.num];
    xianXiaDetail *xxd=[xianXiaDetail shardeManager];
    progress=0.0f;
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
    if (xxd.toTheNextView==1)
    {
        [self updateDataFromNewNet];
        xxd.toTheNextView=0;
        self.whichView=1;
    }
    else
    {
        
        [self updateDataFromNet];
        self.whichView=2;
    }
    [self changeViewColor];
    float height=self.headerView01.frame.origin.y+self.headerView01.frame.size.height;
    self.backView=[[UIView alloc]initWithFrame:CGRectMake(0, height, screenWidth, screenHeight-height)];
    self.backView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.backView];
}
-(void)changeViewColor
{
    //导航条
    self.headerView.backgroundColor=[UIColor colorWithRed:243/255.0 green:143/255.0 blue:177/255.0 alpha:1.0];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1.0];
    self.view.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0];
    self.purchaseBtn.backgroundColor=[UIColor colorWithRed:216/255.0 green:26/255.0 blue:96/255.0 alpha:1.0];
}
-(void)updateDataFromNewNet
{
//    buyNSObject*bn=[buyNSObject shardeManager];
    xianXiaDetail *xxd=[xianXiaDetail shardeManager];
    NSString *urlString=@"http://112.74.114.1/Parenting/index.php/api/lineAction/order";
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"uid"]=userID;
    parameters[@"ticket_id"]=xxd.ticket_idString;
    parameters[@"id"]=xxd.idString;
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         data_dic=[NSDictionary dictionary];
         data_dic=[json objectForKey:@"data"];
//         NSLog(@"data=%@",data_dic);
         if (data_dic.count>0)
         {
//             bn.user_tel=[data_dic objectForKey:@"phone"];
//             bn.ticket_price=[data_dic objectForKey:@"tprice"];
//             bn.nameString=[data_dic objectForKey:@"seller"];
//             bn.lidString=[data_dic objectForKey:@"lid"];
//             bn.tnameString=[data_dic objectForKey:@"tname"];
//             self.nameLabel.text=bn.nameString;
//             self.phoneNumberLabel.text=bn.user_tel;
//             self.priceLabel01.text=bn.ticket_price;
//             self.priceLabel02.text=bn.ticket_price;
//             self.priceLabel03.text=bn.ticket_price;
//             self.priceLabel04.text=bn.ticket_price;
//             buyNSObject*bn=[buyNSObject shardeManager];
//             bn.all_price=self.priceLabel04.text;
             [self dismiss01];
         }
//        NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
-(void)updateDataFromNet
{
    tiaoZhuanView *tzv=[tiaoZhuanView shardeManager];
    nextActivityModel*nam=[nextActivityModel shardeManager];
    if (tzv.numViewController==1)
    {
        self.ticket_IDString= nam.ticket_id;
    }
    else if (tzv.numViewController==2)
    {
        self.ticket_IDString=nam.ticket_id;
    
    }
    swimToPhoto *stp=[swimToPhoto shardeManager];
    if (stp.num==1)
    {
        self.urlString1=[NSString stringWithFormat:@"http://112.74.114.1/parenting/index.php/Api/Active/photo_order"];
    }
    else if (stp.num==2)
    {
        self.urlString1=[NSString stringWithFormat:@"http://112.74.114.1/parenting/Api/Active/swim_order"];
    }
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"user_id"]=@"1";
    parameters[@"ticket_id"]=self.ticket_IDString;
    parameters[@"active_id"]=nam.idString;

    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:self.urlString1 parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //获取数组
         json_Array1=[json objectForKey:@"data"];
         buyNSObject*bn=[buyNSObject shardeManager];
         if (json_Array1.count>0)
         {
//             bn.user_tel=[json_Array1[0] objectForKey:@"user_tel"];
//             bn.ticket_price=[json_Array1[1] objectForKey:@"ticket_price"];
//             bn.nameString=[json_Array1[2] objectForKey:@"active_title"];
//             self.nameLabel.text=bn.nameString;
//             self.phoneNumberLabel.text=bn.user_tel;
//             self.priceLabel01.text=bn.ticket_price;
//             self.priceLabel02.text=bn.ticket_price;
//             self.priceLabel03.text=bn.ticket_price;
//             self.priceLabel04.text=bn.ticket_price;
//             buyNSObject*bn=[buyNSObject shardeManager];
//             bn.all_price=self.priceLabel04.text;
             [self dismiss02];
         }
//           NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)increaseProgress01
{
    progress += 0.05f;
    [SVProgressHUD showProgress:progress status:@"加载中..."];
    
    if(progress < 1.0f){
        [self performSelector:@selector(increaseProgress01) withObject:nil afterDelay:0.1f];
    } else {
        [self performSelector:@selector(dismiss01) withObject:nil afterDelay:0.4f];
    }
}
- (void)increaseProgress02
{
    progress += 0.05f;
    [SVProgressHUD showProgress:progress status:@"加载中..."];
    
    if(progress < 1.0f){
        [self performSelector:@selector(increaseProgress02) withObject:nil afterDelay:0.1f];
    } else {
        [self performSelector:@selector(dismiss02) withObject:nil afterDelay:0.4f];
    }
}
- (void)dismiss01
{
    [SVProgressHUD dismiss];
    self.backView.hidden=YES;
    buyNSObject*bn=[buyNSObject shardeManager];
    bn.user_tel=[data_dic objectForKey:@"phone"];
    bn.ticket_price=[data_dic objectForKey:@"tprice"];
    bn.nameString=[data_dic objectForKey:@"seller"];
    bn.lidString=[data_dic objectForKey:@"lid"];
    bn.tnameString=[data_dic objectForKey:@"tname"];
    self.nameLabel.text=bn.nameString;
    self.phoneNumberLabel.text=bn.user_tel;
    self.priceLabel01.text=bn.ticket_price;
    self.priceLabel02.text=bn.ticket_price;
    self.priceLabel03.text=bn.ticket_price;
    self.priceLabel04.text=bn.ticket_price;
    bn.all_price=self.priceLabel04.text;

}
- (void)dismiss02 {
    [SVProgressHUD dismiss];
    self.backView.hidden=YES;
    buyNSObject*bn=[buyNSObject shardeManager];
    bn.user_tel=[json_Array1[0] objectForKey:@"user_tel"];
    bn.ticket_price=[json_Array1[1] objectForKey:@"ticket_price"];
    bn.nameString=[json_Array1[2] objectForKey:@"active_title"];
    self.nameLabel.text=bn.nameString;
    self.phoneNumberLabel.text=bn.user_tel;
    self.priceLabel01.text=bn.ticket_price;
    self.priceLabel02.text=bn.ticket_price;
    self.priceLabel03.text=bn.ticket_price;
    self.priceLabel04.text=bn.ticket_price;
    bn.all_price=self.priceLabel04.text;
}
- (IBAction)goBack:(id)sender
{
    if (self.whichView==1)
    {
        //跳到指定的界面
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:[xianXiaDetailViewController class]])
            {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    }
    else
    {
        babySwimViewController *bsvc=self.navigationController.viewControllers[1];
        [self.navigationController popToViewController:bsvc animated:YES];
    }

}
//减符号的按钮
- (IBAction)minius:(id)sender
{
    self.num--;
    if (self.num<1)
    {
        self.num=1;
    }
    self.textField.text=[NSString stringWithFormat:@"%d",self.num];
    float allPrice=[self.priceLabel01.text floatValue]*self.num;
    self.priceLabel02.text=[NSString stringWithFormat:@"%0.2f",allPrice];
    self.priceLabel03.text=[NSString stringWithFormat:@"%0.2f",allPrice];
    self.priceLabel04.text=[NSString stringWithFormat:@"%0.2f",allPrice];
    buyNSObject*bn=[buyNSObject shardeManager];
    bn.all_price=self.priceLabel04.text;
//    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
//    [userDefaults setValue:self.priceLabel04.text forKey:@"allMoney"];

}
//加符号的按钮
- (IBAction)plus:(id)sender
{
    self.num++;
    self.textField.text=[NSString stringWithFormat:@"%d",self.num];
    float allPrice=[self.priceLabel01.text floatValue]*self.num;
    self.priceLabel02.text=[NSString stringWithFormat:@"%0.2f",allPrice];
    self.priceLabel03.text=[NSString stringWithFormat:@"%0.2f",allPrice];
    self.priceLabel04.text=[NSString stringWithFormat:@"%0.2f",allPrice];
    buyNSObject*bn=[buyNSObject shardeManager];
    bn.all_price=self.priceLabel04.text;

}
//立即购买按钮
- (IBAction)purchase:(id)sender
{
    if(self.whichView==1)
    {
        [self passDataToNet];
    }
    else
    {
        swimToPhoto *stp=[swimToPhoto shardeManager];
        if (stp.num==1)
        {
            self.urlString2=[NSString stringWithFormat:@"http://112.74.114.1/parenting/index.php/Api/Active/insert"];
        }
        else if (stp.num==2)
        {
            self.urlString2=[NSString stringWithFormat:@"http://112.74.114.1/parenting/Api/Active/insert"];
        }
        nextActivityModel *nam=[nextActivityModel shardeManager];
        NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
        parameters[@"user_id"]=@"1";
        parameters[@"ticket_id"]=self.ticket_IDString;
        parameters[@"ticket_type"]=nam.type;
        parameters[@"ticket_num"]=[NSString stringWithFormat:@"%d",self.num];
        
        HttpRequest *httpRequest=[HttpRequest sharedInstance];
        [httpRequest postWithURLString:self.urlString2 parameters:parameters success:^(id responseObject)
         {
             id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
             //获取数组
             NSString *msg=[json objectForKey:@"msg"];
             if ([msg isEqualToString:@"200"])
             {
                 sureOrderViewController *sovc=[sureOrderViewController new];
                 [self.navigationController pushViewController:sovc animated:YES];
             }
             //        NSLog(@"json=%@",json);
         } failure:^(NSError *error) {
             NSLog(@"error=%@",error);
         }];
    }
}
-(void)passDataToNet
{
    buyNSObject*bn=[buyNSObject shardeManager];
    NSString *urlString=@"http://112.74.114.1/Parenting/index.php/api/lineAction/addorder";
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"uid"]=userID;
    parameters[@"lid"]=bn.lidString;
    parameters[@"seller"]=bn.nameString;
    parameters[@"tname"]=bn.tnameString;
    parameters[@"tprice"]=bn.ticket_price;
//    NSLog(@"bn.ticket_price=%@",bn.ticket_price);
    parameters[@"num"]=[NSString stringWithFormat:@"%d",self.num];
    parameters[@"phone"]=bn.user_tel;
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//         NSLog(@"json000=%@",json);
         NSDictionary *data_dic01=[json objectForKey:@"data"];
         if (data_dic01.count>0)
         {
             activityOrder *ao=[activityOrder shardeManager];
             ao.numberString=[data_dic01 objectForKey:@"number"];
             ao.subjectString=[data_dic01 objectForKey:@"subject"];

             sureOrderViewController *sovc=[sureOrderViewController new];
             [self.navigationController pushViewController:sovc animated:YES];
         }
         //获取数组
//        NSLog(@"json=%@",json);
     } failure:^(NSError *error)
     {
         NSLog(@"error=%@",error);
     }];
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
